// pages/project_user_list/project_user_list.js
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    projectId: 0,
    projectName: '',
    projectStatus: 0, // 只有运行状态的项目，才允许添加负责人
    projectManagerFlag: false, // 是否项目负责人，是负责人才允许添加负责人
    projectUserList: [],
    userId: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      projectId: options.projectId,
      projectName: options.projectName,
      projectStatus: options.projectStatus,
      projectManagerFlag: (options.projectManagerFlag=='true')? true: false,
      userId: wx.getStorageSync('userId')
    });

    wx.setNavigationBarTitle({
      title: options.projectName,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initDataList();
  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '项目负责人'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '项目负责人'
    }
  },

  /**
   * 初始化数据
   */
  initDataList() {
    request.GET("/project/findProjectUserById", {projectId: this.data.projectId}).then(res => {
      // userId,projectUserType,username
      this.setData({
        projectUserList: res.data
      });
    });
  },

  /**
   * 跳转到负责人添加页面
   */
  gotoProjectUserAdd() {
    wx.navigateTo({
      url: '/pages/project_user_add/project_user_add?projectId='+this.data.projectId
    })
  },

  /**
   * 删除负责人
   */
  deleteProjectUser(e) {

    let projectId = this.data.projectId;
    let userId = e.currentTarget.dataset.id;
    let that = this;

    // 删除逻辑
    wx.showModal({
      title: '删除',
      content: '确定删除该项目负责人？',
      success (res) {
        if (res.confirm) {
          request.POST('/project/deleteProjectUserById/'+projectId+'/'+userId, {}).then(res => {
            if (res.statusCode==200) {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 1000
              })
              that.initDataList();
            } else {
              wx.showToast({
                title: '操作失败',
                icon: 'error',
                duration: 1500
              })
            }
          })
        }
      }
    })
  },

})